import { createRouter, createWebHistory } from 'vue-router'

import { useHtmlTitle } from '@/hooks/core/useHtmlTitle'
import { useAppStore } from '@/stores/modules/app'
import LoggerUtils from '@/utils/logger'

import routes from './routes'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach(to => {
  const { setHtmlTitle } = useHtmlTitle()
  setHtmlTitle(to.meta.title)

  if (to.meta.ignoreAuth) {
    return true
  }

  const appStore = useAppStore()
  if (!appStore.isLogined && to.path !== '/sign-in') {
    LoggerUtils.error('未经授权，请先登录')
    return '/sign-in'
  }
})
export default router
